package com.aispeech.testcustom.utils;

import android.media.AudioFormat;
import android.util.Log;

import com.aispeech.testcustom.TiApplication;
import com.aispeech.testcustom.bean.AISampleRate;
import com.aispeech.testcustom.i.TiConst;

import org.apache.commons.collections4.queue.CircularFifoQueue;

import java.io.File;
import java.io.IOException;

/**
 * Created by rain on 2016/12/11.
 */
public class FileUtils {

    /**
     * 删除文件或者清空文件夹
     * 删除成功返回 0
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static int cleanFF(String path){
        File dir = new File(path);
        if (!dir.exists()) {
            return TiConst.NOT_EXIST;
        }
        if (!dir.isDirectory()) {
            if (dir.isFile()) {
                dir.delete();//如果是文件 , 直接删除
            }
        } else {
            String[] tmpList = dir.list();
            File f = null;
            for (String s : tmpList) {
                if (path.endsWith(File.separator)) {
                    f = new File(path + s);
                } else {
                    f = new File(path + File.separator + s);
                }
                if (f.isFile()) {
                    f.delete();
                } else if (f.isDirectory()) {
                    cleanFF(path + File.separator + s);
                }
            }
        }

        return TiConst.CLEAN_FINISH;
    }

    /**
     * 删除唤醒音频
     * @param path
     * @return
     * @throws IOException
     */
    public static int removeWakeupWavFile(String path){
        return cleanFF(SystemUtils.getExternalCacheDir(TiApplication.getContext())
                + File.separator + "wakeup");
    }

    /**
     * 保存误唤醒音频
     * 保存音频名称规则:类型+"_"+时间戳+"_"+唤醒词.wav
     * 保存在/sdcard/Android/data/com.aispeech.xxx/cache/wakeup/
     */
    public static void SaveWavFileFromWakeup(CircularFifoQueue<byte[]> mAecQueue,
                                             CircularFifoQueue<byte[]> mOriginalQueue, String
                                                     name, boolean isAec, long currentTime) {

        if (SystemUtils.getExternalCacheDir(TiApplication.getContext()) == null) {
            //无法写文件
            Log.d("test", "SaveWavFileFromWakeup: cannot getExternalCacheDir");
            return;
        }
        if (isAec) {
            Log.d("test", "SaveWavFileFromWakeup: 保存aec音频");
            String wakeupAecWavPath = SystemUtils.getExternalCacheDir(TiApplication.getContext())
                    + File.separator + "wakeup" + File.separator + "aec_"
                    + currentTime + "_" +
                    name + ".wav";
            WavFileWriter wavAecFileWriter = WavFileWriter.createWavFileWriter(new File
                            (wakeupAecWavPath),
                    AISampleRate.SAMPLE_RATE_16K, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat
                            .ENCODING_PCM_16BIT, false);

            if (wavAecFileWriter != null) {
                for (int i = 0; i < mAecQueue.size(); i++) {
                    wavAecFileWriter.writeWavData(mAecQueue.get(i));
                }
                wavAecFileWriter.closeWav();
            }
        }

        String n = isAec ? "stereo" : "mono";
        String wakeupOriginalWavPath = SystemUtils.getExternalCacheDir(TiApplication.getContext()
        ) + File.separator + "wakeup" + File.separator + n + "_" + currentTime + "_"
                + name + ".wav";
        WavFileWriter wavOriginalFileWriter = WavFileWriter.createWavFileWriter(new File
                        (wakeupOriginalWavPath),
                AISampleRate.SAMPLE_RATE_16K, isAec ? 2 : 1, AudioFormat
                        .ENCODING_PCM_16BIT, false);
        if (wavOriginalFileWriter != null) {
            for (int i = 0; i < mOriginalQueue.size(); i++) {
                wavOriginalFileWriter.writeWavData(mOriginalQueue.get(i));
            }
            wavOriginalFileWriter.closeWav();
        }
    }
}
